﻿#pragma execution_character_set("utf-8")
#include "testsetup.h"
#include "ui_testsetup.h"

TestSetup::TestSetup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestSetup)
{
    ui->setupUi(this);
    model = nullptr;
    this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint);
}

void TestSetup::CreateTestSetup(SetupCase *setupCase, QList<TestCase> *testCaseList)
{
    locked = true;
    this->setupcase = setupCase;
    this->testcaselist = testCaseList;
    ui->label_testFilePath->setText(setupCase->testFile);

    if(model != nullptr){
        model->clear();
        delete model;
    }
    if(!isStartTestCase){
        ui->pushButton_savecur->setEnabled(false);
        ui->pushButton_savefile->setEnabled(false);
        //ui->pushButton_saveas->setEnabled(false);
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }else{
        ui->pushButton_savecur->setEnabled(true);
        //ui->pushButton_savefile->setEnabled(true);
        //ui->pushButton_saveas->setEnabled(true);
    }

    model = new QStandardItemModel;
    model->setColumnCount(9);
    model->setHeaderData(0, Qt::Horizontal, "测试名称");
    model->setHeaderData(1, Qt::Horizontal, "测试命令");
    model->setHeaderData(2, Qt::Horizontal, "运行次数");
    model->setHeaderData(3, Qt::Horizontal, "超时时间");
    model->setHeaderData(4, Qt::Horizontal, "结果前缀");
    model->setHeaderData(5, Qt::Horizontal, "结果后缀");
    model->setHeaderData(6, Qt::Horizontal, "上限值");
    model->setHeaderData(7, Qt::Horizontal, "下限值");
    model->setHeaderData(8, Qt::Horizontal, "单位");
    ui->tableView->setModel(model);
    QCoreApplication::processEvents();

    for(int i=0; i<testCaseList->length(); i++){
        model->setItem(i, 0, new QStandardItem(QString("%1").arg(testCaseList->at(i).name)));
        model->setItem(i, 1, new QStandardItem(QString("%1").arg(testCaseList->at(i).cmd)));
        model->setItem(i, 2, new QStandardItem(QString("%1").arg(testCaseList->at(i).counts)));
        model->setItem(i, 3, new QStandardItem(QString("%1").arg(testCaseList->at(i).timeMax)));
        model->setItem(i, 4, new QStandardItem(QString("%1").arg(testCaseList->at(i).resultStart)));
        model->setItem(i, 5, new QStandardItem(QString("%1").arg(testCaseList->at(i).resultEnd)));
        model->setItem(i, 6, new QStandardItem(QString("%1").arg(testCaseList->at(i).resultUpper)));
        model->setItem(i, 7, new QStandardItem(QString("%1").arg(testCaseList->at(i).resultLower)));
        model->setItem(i, 8, new QStandardItem(QString("%1").arg(testCaseList->at(i).unitStr)));
    }
    QCoreApplication::processEvents();
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
    locked = false;

    this->resizeEvent(nullptr);
    this->setVisible(true);
}

TestSetup::~TestSetup()
{
    delete ui;
}

void TestSetup::resizeEvent(QResizeEvent *)
{
    if(locked)
        return;
    int counts = model->columnCount();          //获取表格的列数
    int widths = ui->tableView->width();
    int colWidthsAve = static_cast<int>(widths * 1.0 / counts);
    for(int i=0; i<counts; i++){
      ui->tableView->setColumnWidth(i, colWidthsAve);
    }
    //ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
}
